package com.delivery.www.service.ServiceImpl;

import com.delivery.www.dao.OrderDao;
import com.delivery.www.po.Dish;
import com.delivery.www.po.Order;
import com.delivery.www.service.OrderService;
import com.delivery.www.dao.DaoImpl.OrderDaoImpl;

import java.util.List;

/**
 * @author 12970
 */
public class OrderServiceImpl implements OrderService {
    private final OrderDao orderDao = new OrderDaoImpl();

    @Override
    public boolean addOrder(int userId, int storeId, int dishId, Float money, int couponId) {
        if (userId <= 0 || storeId <= 0 || dishId <= 0 || money < 0 || couponId < 0) {
            return false;
        } else {
            return orderDao.addOrder(userId, storeId, dishId, 0, money, couponId) > 0;
        }
    }

    @Override
    public List<Order> getCompleteOrder(int storeId, int status) {
        if (storeId <= 0 || status < 0) {
            return null;
        } else {
            return orderDao.getCompleteOrder(storeId, status);
        }
    }

    @Override
    public List<Order> getUnCompleteOrder(int storeId, int status) {
        if (storeId <= 0 || status < 0) {
            return null;
        } else {
            return orderDao.getUnCompleteOrder(storeId, status);
        }
    }

    @Override
    public boolean modifyStatus(int orderId, int status, int storeId) {
        if (orderId <= 0 || status < 0 || storeId <= 0) {
            return false;
        } else {
            return orderDao.modifyStatus(orderId, status, storeId) > 0;
        }
    }

    @Override
    public List<Order> getOwnOrder(int userId) {
        if (userId <= 0) {
            return null;
        } else {
            return orderDao.getOwnOrder(userId);
        }
    }

    @Override
    public boolean cartOrder(int userId, List<Integer> dishId) {
        if (userId <= 0) {
            return false;
        } else {
            List<Integer> integers = orderDao.cartOrder(userId, dishId);
            boolean b = true;
            for (Integer integer : integers) {
                if (integer <= 0) {
                    b = false;
                    break;
                }
            }
            return b;
        }
    }

    @Override
    public Float judgeBalance(List<Integer> dishIds) {
        if (dishIds == null) {
            return 0F;
        }else {
            DishServiceImpl dishService = new DishServiceImpl();
            Float money = 0F;
            for (Integer dishId : dishIds) {
                Dish dish = dishService.getOneDish(dishId);
                Float price = dish.getPrice();
                money += price;
            }
            return money;
        }
    }
}
